package com.client.view;

import java.awt.Color;







import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Toolkit;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.client.ctrl.RoatCtrl;
import com.entity.Collage;
import com.util.CommonUtil;
import com.util.MyTable;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;

public class CollageDialog extends JDialog{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	private final JPanel contentPanel = new JPanel();
	private JTextField collageIdField;
	private JTextField nameField;
	
	private Collage collage;
	RoatCtrl roatCtrl=new RoatCtrl();
	public CollageDialog(final Collage collage) {
		getContentPane().setBackground(SystemColor.inactiveCaptionBorder);
		this.collage=collage;
	}
	
   
	/**
	 * Launch the application.
//	 */
//	public static void main(String[] args) {
//		try {
//			collage collage=new collage();
//			collageDialog dialog = new collageDialog(collage);
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	/**
	 * Create the dialog.
	 */
	public void createCollageDialog(final MyTable table) {
		
		setIconImage(Toolkit.getDefaultToolkit().getImage("src\\com\\util\\Logal.jpg"));
		setBounds(100, 100, 800, 600);
		setTitle("学院信息");
		getContentPane().setLayout(null);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPanel.setBackground(new Color(245,245,226));
		contentPanel.setBounds(0,0,800,400);
		getContentPane().add(contentPanel);
		contentPanel.setLayout(null);
		
		JLabel collageIdLabel = new JLabel("学院编码:");
		CommonUtil.setFont(collageIdLabel, 5);
		collageIdLabel.setBounds(150,160, 100, 30);
		contentPanel.add(collageIdLabel);
		
		collageIdField = new JTextField();
		collageIdField.setEditable(false);
		if(collage.getCollageNo()!=null){
			collageIdField.setText(collage.getCollageNo());
		}else{
			collageIdField.setText("cl"+CommonUtil.getRandom(2));
		}
		
		CommonUtil.setFont(collageIdField, 6);
		collageIdField.setBounds(250, 160, 300, 40);
		contentPanel.add(collageIdField);
		collageIdField.setColumns(10);
		
		JLabel collageNamelabel = new JLabel("学院名称:");
		CommonUtil.setFont(collageNamelabel, 5);
		collageNamelabel.setBounds(150, 220, 100, 30);
		contentPanel.add(collageNamelabel);
		
		nameField = new JTextField();
		if(collage.getName()!=null){
			nameField.setText(collage.getName());
		}
		CommonUtil.setFont(nameField, 6);
		nameField.setColumns(10);
		nameField.setBounds(250, 220, 300, 40);
		contentPanel.add(nameField);
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBackground(new Color(245,245,226));
			buttonPane.setBounds(0, 400, 800, 200);
			getContentPane().add(buttonPane);
			buttonPane.setLayout(null);
			
				final JButton okButton = new JButton("保存");
				CommonUtil.setFont(okButton, 7);
				okButton.setBackground(new Color(142,240,146));
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						String collageNo=collageIdField.getText();
						String name=nameField.getText();
						  if( name==null || "".equals(name.trim()) ){
							JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='red'>学院名称不能为空</font></h></html>"),"错误信息",JOptionPane.ERROR_MESSAGE);
						}else{
							roatCtrl.saveCollage(okButton, table,contentPanel, collage.getCampusNo(),collageNo, name);
							setVisible(false);
						}
						
					}
				});
				okButton.setLocation(250,10);
				okButton.setSize(100, 30);
				if(collage.getCollageNo()==null){
					okButton.setActionCommand("add");
				}else{
					okButton.setActionCommand("update");
				}
				
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			
			
				JButton cancelButton = new JButton("取消");
				CommonUtil.setFont(cancelButton, 7);
				cancelButton.setBackground(new Color(209,138,249));
				cancelButton.setLocation(400,10);
				cancelButton.setActionCommand("Cancel");
				cancelButton.setSize(100, 30);
				cancelButton.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
						
					}
				});
				buttonPane.add(cancelButton);
			
		}
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	    setVisible(true);
		
	}


	


	
}
